package com.lc.zpyh.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lc.zpyh.R;
import com.lc.zpyh.http.response.SelectCanteenAndSupermarketBySchoolIdBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ShiTangPopupWindow extends PopupWindow {
    private View mPopView;
    private List<SelectCanteenAndSupermarketBySchoolIdBean.ListBean> dataList=new ArrayList();

    private JiGuiPopupWindow.OnItemLickenter onItemLickenter;
    private RecyclerView recyclerView;

    private BaseQuickAdapter<SelectCanteenAndSupermarketBySchoolIdBean.ListBean, BaseViewHolder> adapter;
    private Context context;

    public ShiTangPopupWindow(Context context, List dataList) {
        super(context);
        this.context = context;
        this.dataList = dataList;

        init(context);
        setPopupWindow();

    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mPopView = inflater.inflate(R.layout.pop_jigui, null);

        recyclerView = mPopView.findViewById(R.id.recyclerView);
        adapter = new BaseQuickAdapter<SelectCanteenAndSupermarketBySchoolIdBean.ListBean, BaseViewHolder>
                (R.layout.item_popwindow_jigui, dataList) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder, SelectCanteenAndSupermarketBySchoolIdBean.ListBean dataBean) {
                baseViewHolder.setText(R.id.tv_name, dataBean.getName());
//                if (dataBean.getCabinetryId()==cabinetryId){
//                    baseViewHolder.setTextColor(R.id.tv_name,context.getResources().getColor(R.color.colorec6a0d));
//                }else {
//                    baseViewHolder.setTextColor(R.id.tv_name,context.getResources().getColor(R.color.color333333));
//                }
            }


        };

        adapter.setOnItemClickListener(new  OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (onItemLickenter != null) {
                    onItemLickenter.onTypeClick(dataList.get(position).getName() ,dataList.get(position).getId()+"");

                }
                dismiss();
            }
        });

        recyclerView.setAdapter(adapter);
    }

    @SuppressLint("InlinedApi")
    private void setPopupWindow() {

        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
//        this.setAnimationStyle(R.style.DetailAnimation);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        this.setOutsideTouchable(true);
//
//        WindowManager.LayoutParams lp =((Activity) context).getWindow().getAttributes();
//                 lp.alpha = 0.7f;
//        ((Activity) context).getWindow().setAttributes(lp);
        this.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
                lp.alpha = 1f;
                ((Activity) context). getWindow().setAttributes(lp);
            }
        });

    }



    public void setOnItemLickenter(JiGuiPopupWindow.OnItemLickenter onItemLickenter) {
        this.onItemLickenter = onItemLickenter;
    }

    public interface OnItemLickenter {
        void onTypeClick(String type,String id);

    }
}
